14. Vectors in Python
Vectors in Python
In Python, you can represent a vector with lists. So a vector like
\begin{bmatrix}5, 9, 10, 2, 20\end{bmatrix}
could be represented with
myvector = [5, 9, 10, 2, 20]
Vector Indexing
If you wanted to access values inside the vector, you would use indexing. The first value, which in this case is 5, would be accessed by
myvector[0]
The second value:
```python
myvector[1]
And so on.
#### Assigning Values to Vectors
If you wanted to change a value in the vector, you could use this syntax:
python
myvector[3] = 19
which would change the fourth element from 20 to 19.
To add a value to the end of a vector, you can use the .append() method:
python
myvector = [5, 9, 2, 20]
myvector.append(18)
```
and the resulting vector would be [5, 9, 2, 20, 18].
Vector Math in Python
There are a few vector operations you'll want to become familiar with in order to use the Kalman filter equations:
- vector addition
- scalar multiplication
- the dot product
Vector Addition
To add two vectors together, you sum them element by element. For example,
\mathbf{v_1} = [a, b, c, d]
\mathbf{v_2} = [w, x, y, z]
The sum of these two vectors would then be:
\mathbf{v_1} + \mathbf{v_2} = [a + w, b + x, c + y, d + z]
Say that you know the current state of your vehicle \mathbf{x} = [x, y, v_x, v_y]
If you knew the change in the position and velocity of your vehicle, you could use vector addition to find the new state vector. You will code this in the vector coding exercises.
Vector addition Python Code
How might you execute vector addition using Python? In general, for loops are very useful for accessing values inside of a Python list:
for i in range(len(v1)):
v1[i]
would give you access to v1, one element at a time. So you could access each value of v1 and v2, sum them together, and then append each sum to a new vector like:
# initialize an empty vector
vsum = []
# use for loops to take the sum of a value and then append to vsum
for ....
.....
vsum.append(value)
We are not showing you the full answer because you will be figuring this out in a coding exercise.
Scalar Multiplication
Scalar multiplication involves multiplying each element in a vector by a constant.
Here is a concrete example.
If \mathbf{v} = [a, b, c, d], then 5\mathbf{v} = [5a, 5b, 5c, 5d]
You will also implement scalar multiplication in the coding exercises. Think about how you can use a for loop and the append method to code scalar multiplication.
A use case for scalar multiplication could be changing between units of measurement. For example, if your state were measured in meters, you could use scalar multiplication to convert to feet. Get ready to code this in the vector coding exercises!
Dot Product
The dot product of two vectors is very important for matrix multiplication. If
\mathbf{v_1} = [a, b, c, d]
\mathbf{v_2} = [w, x, y, z],
then the dot product would be
\mathbf{v_1 \cdot v_2} = aw+ bx + cy + dz
This is another place where a for loop would be useful. You will figure out how to code the dot product in the exercises in the next section.
You'll see how to apply the dot product and why it is useful in the coding exercises.
Summary
Here is a summary of what you will need to know about Python lists to complete the coding exercises:
- assigning a vector -
myvector = [5, 9, 10 2, 20]
- accessing a value -
myvector[3]
- changing a value -
myvector[3] = 15
- appending a value to the end of the list -
myvector.append(6)
- and finally, using for loops to access and manipulate vectors:
for i in range(len(myvector)):
myvector[i]
Check out this link to learn about other methods that come with Python lists. You might find them useful although you will not need them for the following exercises.
Next, you will practice coding vectors in Python.